package com.dk.controller;

import com.dk.api.UserServiceAPI;
import com.dk.pojo.Warehouse;
import com.dk.result.PageResult;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import com.dk.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("warehouse")
@Api(tags = "仓库接口文档")
@RefreshScope
public class WarehouseController {

    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private UserServiceAPI userServiceAPI;

    @GetMapping("getWarehouseInfo")
    @ApiOperation("仓库模糊查询和分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数"),
            @ApiImplicitParam(name = "code", value = "仓库编号"),
            @ApiImplicitParam(name = "name", value = "仓库名称")
    })
    public Result getWarehouseInfo(@RequestParam(defaultValue = "1")Integer pageNum,
                                   @RequestParam(defaultValue = "5") Integer pageSize,
                                   String code, String name){
        PageResult<Warehouse> result = warehouseService.getWarehouseInfo(pageNum,pageSize,code,name);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE,result);
    }

    @PostMapping("addWarehouse")
    @ApiOperation("仓库增加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "仓库编号",required = true),
            @ApiImplicitParam(name = "name", value = "仓库名称",required = true),
            @ApiImplicitParam(name = "location", value = "仓库位置",required = true),
            @ApiImplicitParam(name = "area", value = "仓库面积",required = true),
            @ApiImplicitParam(name = "head", value = "仓库负责人"),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true),
            @ApiImplicitParam(name = "remark", value = "备注")
    })
    public Result addWarehouse(Warehouse warehouse,@RequestHeader String token){
        Result result = userServiceAPI.selectUserAll();
        List<String> users = (List<String>) result.getData();
        if(!users.contains(warehouse.getHead())){
            return new Result(ResultConstant.CODE_ERROR,ResultConstant.MESSAGE_ERROR);
        }
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        Result result1 = warehouseService.addWarehouse(warehouse, token);
        return result1;
    }

    @PutMapping("updateWarehouse")
    @ApiOperation("仓库修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wid", value = "仓库id",required = true),
            @ApiImplicitParam(name = "code", value = "仓库编号"),
            @ApiImplicitParam(name = "name", value = "仓库名称"),
            @ApiImplicitParam(name = "location", value = "仓库位置"),
            @ApiImplicitParam(name = "area", value = "仓库面积"),
            @ApiImplicitParam(name = "head", value = "仓库负责人",required = true),
            @ApiImplicitParam(name = "token", value = "登录后返回的token串",required = true),
            @ApiImplicitParam(name = "remark", value = "备注")
    })
    public Result updateWarehouse(Warehouse warehouse,@RequestHeader String token){
        //查询出用户,名的所有信息
        Result result = userServiceAPI.selectUserAll();
        List<String> users = (List<String>) result.getData();
        //输入的是数据库没有的
        if(!users.contains(warehouse.getHead())){
            return new Result(ResultConstant.CODE_ERROR,ResultConstant.MESSAGE_ERROR);
        }
        //token不符合规范
        if(token == null || "".equals(token)){
            return new Result(ResultConstant.TOKEN_CODE, ResultConstant.TOKEN_MESSAGE);
        }
        Result result1 = warehouseService.updateWarehouse(warehouse, token);
        return result1;
    }

    @DeleteMapping("deleteWarehouse")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "wid",value = "仓库id",required = true)
    })
    public Result deleteWarehouse(Integer wid){
        warehouseService.deleteWarehouse(wid);
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE);
    }

    @GetMapping("selectByWarehouseName")
    public Warehouse selectByWarehouseName(@RequestParam String warehouseName){
        return warehouseService.selectByWarehouseName(warehouseName);
    }
}